﻿$(document).ready(function() {

    $("div.postList table tr td.postDescriptionCell a")
        .attr("href", "#")
        .click(onDetailsLinkClicked);
});

function onDetailsLinkClicked(evt) {
    evt.preventDefault();

    var currentRowElement = $(this).parent().parent();

    collapseDetailsForAllRows();

    var row = createRowObject(currentRowElement);
    
    row.hideDetailsCells();
   
    if (!row.isSelected) {
        row.select();
        row.fadeInDetailsCells();
    }
}

function createRowObject(row) {
    var rowObject = {
        rowElement : row,
        actionsCell : row.children(".postActionsCell"),
        tagsCell : row.children(".postTagsCell"),
        commentCell : row.children(".postCommentCell"),
        isSelected : row.hasClass("selected"),
        
        select : function () {
            this.rowElement.addClass("selected");
        },
        
        hideDetailsCells : function () {
            this.actionsCell.hide();
            this.tagsCell.hide();
            this.commentCell.hide();
        },
        
        fadeInDetailsCells : function () {
            this.actionsCell.fadeIn(150);
            this.tagsCell.fadeIn(150);
            this.commentCell.fadeIn(150);
        }
    };

    return rowObject;
}

function collapseDetailsForAllRows() {
    $("div.postList table tr")
        .each(function(id) {
            clearInlineDisplayStyleForAllChildren($(this));
        })
        .removeClass("selected");
    }

function clearInlineDisplayStyleForAllChildren(elements) {
    elements.children().each(function() {
        this.style.display = "";
    });
}